// pages/login/login.js
const AUTH = require('../../utils/auth')
const API = require('../api/index')
const {
  get,
  post
} = require('../../request/index')
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';

let timer;
Page({

  data: {
    disableds:false,
    showProtocol: true,
    fromShow: true,
    disabled: true,
    agree: false,
    viewShow: false,
    show: false,
    loginShow: false,
    showOneButtonDialog: false,
    oneButton: [{
      text: '确定'
    }],
    loginuser: "注册",
    error: '',
    formData: {

    },
    name: '所有城市',
    rules: [{
        name: 'nickName',
        rules: {
          required: true,
          message: '用户名是必填项'
        },
      },
      {
        name: 'pwd',
        rules: {
          required: true,
          message: '密码是必填项'
        },
      },
      {
        name: 'idCard',
        rules: {
          required: true,
          message: '身份证是必填',
          minlength: 18,
          maxlength: 18
        }
      },
      {

        rules: {
          required: true,
          message: '矿山是必填项'
        },
      },




    ],
    value: "",
    seachList: [],
    mineId: null,
    mineName: "请选择所属矿山",
    lock: true,
    openid: ""
  },
  onLoad(options) {
    console.log(options);
    this.setData({
      openid: options.openid
    })
  },

  checkboxChange() {
    this.data.agree = true
  },
  seachCell(e) {
    this.setData({
      show: false,
      fromShow: true,
      mineId: e.target.dataset.id,
      mineName: e.target.dataset.title
    });
  },


  onChange(e) {

    clearTimeout(timer);
    timer = setTimeout(async () => {
      let data = await post(`/app/mineinfo/getList`, {
        mineName: e.detail
      })

      this.setData({
        viewShow: true,
        seachList: data.data.list
      })
    }, 500);


  },

  tapOneDialogButton(e) {
    this.setData({
      showOneButtonDialog: false
    })
  },
  blurChange(e) {
    //姓名只能是中文
    let ch = /^[\u4e00-\u9fa5]+$/;
    //手机号吗是否合法
    let reg = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
    // 校验身份证号码是否合法
    let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    let _IDre15 = /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
    if (e.target.dataset.field == 'nickName') {
      if (e.detail.value != "") {
        if (!ch.test(e.detail.value)) {
          this.setData({
            error: '请输入合法的中文'
          })
        }
      }

    }

    if (e.target.dataset.field == 'memberName') {
      if (e.detail.value == "") {
        this.setData({
          error: '账号不能为空！！！'
        })
      }
    }

    // 校验身份证：


    if (e.target.dataset.field == 'idCard') {
      if (e.detail.value != "") {
        if (!_IDRe18.test(e.detail.value)) {

          this.setData({
            error: '请输入合法的身份证号码'
          })

        }
      }

    }

    if (e.target.dataset.field == 'mobile') {
      if (e.detail.value != "") {
        if (!reg.test(e.detail.value)) {
          this.setData({
            error: '请输入合法手机号'
          })
        }
      }

    }


  },

  // 矿山弹框
  fromChange(e) {
    this.setData({
      show: true,
      fromShow: false,
      disableds:true
    });

  },
  // 关闭矿山弹框
  onClose() {
    this.setData({
      show: false,
      fromShow: true,
    });
  },
  // input输入时间
  formInputChange(e) {

    const {
      field
    } = e.currentTarget.dataset

    this.setData({
      [`formData.${field}`]: e.detail.value
    })
  },

  // 注册
  getPhoneNumber(e) {

    const {
      nickName,
      memberName,
      pwd,
      idCard,
    } = this.data.formData

    if (this.data.agree == false) {
      wx.showToast({
        title: '请勾阅读并同意',
        icon: 'none',
        duration: 3000,
        mask: true
      })
      return false

    } else {
      this.selectComponent('#form').validate((valid, errors) => {
        if (!valid) {
          const firstError = Object.keys(errors)
          if (firstError.length) {
            this.setData({
              error: errors[firstError[0]].message
            })

          }
        } else {

          if (this.data.mineId != null) {
            this.setData({
              loginShow: true,
              loginuser: "注册中..."
            })
            // 是否重名
            post('/app/asysrole/checkMember', {
              nickName: nickName,
              memberName: memberName,
              mineId: this.data.mineId
            }).then(res => {
              if (!res.success) {
                Notify({
                  color: '#ad0000',
                  background: '#ffe1e1',
                  message: res.msg
                });
                this.setData({
                  loginShow: false,
                  loginuser: "注册"
                })

              } else {

                post(`/wx/wx/register`, {
                  memberName: memberName,
                  nickName: nickName,
                  pwd: pwd,
                  idCard: idCard,
                  mineId: this.data.mineId,
                  wxOpenId: this.data.openid
                }).then(res => {

                  if (res.status == 500) {
                    wx.showToast({
                      title: '网络错误，无法连接',
                      icon: 'error',
                      duration: 2000,
                      mask: true
                    })
                    this.setData({
                      loginShow: false,
                      loginuser: "注册"
                    })
                  }
                  if (res.success === true) {

                    wx.showToast({
                      title: '请联系管理员',
                      icon: 'success',
                      duration: 3000,
                      mask: true
                    })
                    this.setData({
                      loginShow: false,
                      loginuser: "注册成功"
                    })
                    wx.navigateBack({
                      delta: 1
                    })


                  }
                })
              }
            })
          } else {

            Notify({
              color: '#ad0000',
              background: '#ffe1e1',
              message: '矿山是必填项'
            });
          }




        }
      })
    }








  }


})